-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_10.ex
38 lines (32 loc) · 1006 Bytes
/
day_10.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
defmodule AdventOfCode.Day10 do
def part1(args) do
cycles = calculateStateDuringCycles(args)
interest = [20, 60, 100, 140, 180, 220]
interest |> Enum.map(&(Enum.at(cycles, &1) * &1)) |> Enum.sum()
end
def part2(args) do
cycles = calculateStateDuringCycles(args)
cycles
|> Enum.with_index()
|> tl()
|> Enum.map(fn {p, c} -> renderPixel(p, c) end)
|> Enum.chunk_every(40)
|> Enum.map(&Enum.join/1)
end
def renderPixel(p, cycle) do
c = Integer.mod(cycle, 40)
if p == c or p + 1 == c or p + 2 == c, do: "#", else: "."
end
def instructionToCycles("noop", [hd | tail]), do: [hd, hd | tail]
def instructionToCycles(inst, [hd | tail]) do
val = String.split(inst) |> List.last() |> String.to_integer()
[val + hd, hd, hd | tail]
end
def calculateStateDuringCycles(args) do
args
|> String.trim()
|> String.split("\n")
|> Enum.reduce([1, 0], fn x, acc -> instructionToCycles(x, acc) end)
|> Enum.reverse()
end
end